package leetcode.bit;
//191. 位1的个数
public class HammingWeight {
    public static void main(String[] args) {
        System.out.println(hammingWeight2(521));
    }
    //JDK
    public static int hammingWeight1(int n) {
        return Integer.bitCount(n);
    }

    //位运算
    public static int hammingWeight2(int n) {
        int res=0;
        while (n!=0){
            n=n&(n-1);
            res++;
        }
        return res;
    }
}
